﻿using Dtos;
using IBLL;
using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class TreachersBll : ITreachersBll
    {
        private ITreachersDal _dal;
        public TreachersBll(ITreachersDal dal)
        {
            _dal = dal;
        }

        public async Task<int> AddTreachersAsync(string title, DateTime birthday, string gender, string tel, string email, string qqNumber, string address, string pwd, string photo, Guid positionsId, DateTime updateTime)
        {
            return await _dal.AddAsync(new Model.Teachers 
            {
                Name = title,
                Birthday = birthday,
                Gender = gender,
                Tel = tel,
                Email = email,
                Photo = photo,
                PositionsId = positionsId,
                UpdateTime = updateTime,
                QQNumber = qqNumber,
                Address = address,
                Pwd = pwd
            });
        }

        public async Task<int> DeleteTreachersAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            return data != null ? await _dal.DeleteAsync(data) : 0;
        }

        public async Task<List<TreachersDto>> GetAll()
        {
            var data = await _dal.GetAll();
            return data.Select(t => new TreachersDto
            {
                Id = t.Id,
                Name = t.Name,
                Birthday = t.Birthday,
                Gender = t.Gender,
                Tel = t.Tel,
                Email = t.Email,
                Photo = t.Photo,
                PositionsId = t.PositionsId,
                QQNumber = t.QQNumber,
                Address = t.Address,
                Pwd = t.Pwd,
                CreateTime = t.CreateTime,
                UpdateTime = t.UpdateTime,
            }).ToList();
        }

        public async Task<TreachersDto> GetTreachersByIdAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            return new TreachersDto 
            {
                Id = data.Id,
                Name = data.Name,
                Birthday = data.Birthday,
                Gender = data.Gender,
                Tel = data.Tel,
                Email = data.Email,
                Photo = data.Photo,
                PositionsId = data.PositionsId,
                QQNumber = data.QQNumber,
                Address = data.Address,
                Pwd = data.Pwd,
                CreateTime = data.CreateTime,
                UpdateTime = data.UpdateTime,
            };
        }

        public async Task<List<TreachersDto>> GetTreachersByNameAsync(string title)
        {
            var data = await _dal.GetDataByChoose(a => a.Name.Contains(title));
            return data == null ? null :
                data.Select(t => new TreachersDto 
                {
                    Id=t.Id,
                    Name=t.Name,
                    Birthday=t.Birthday,
                    Gender=t.Gender,
                    Tel=t.Tel,
                    Email =t.Email,
                    QQNumber=t.QQNumber,
                    Address=t.Address,
                    Pwd=t.Pwd,
                    Photo=t.Photo,
                    PositionsId=t.PositionsId,
                    CreateTime=t.CreateTime,
                    UpdateTime = t.UpdateTime
                }).OrderByDescending(g => g.UpdateTime).ToList();
        }

        public async Task<int> UpdateTreachersAsync(Guid id, string title, DateTime birthday, string gender, string tel, string email, string qqNumber, string address, string pwd, string photo, Guid positionsId, DateTime updateTime)
        {
            var data = await _dal.GetById(id);
            if(data == null)
                return 0;
            data.Name = title;
            data.Birthday = birthday;
            data.Gender = gender;
            data.Tel = tel;
            data.Email = email;
            data.QQNumber = qqNumber;
            data.Address = address;
            data.Pwd = pwd;
            data.Photo = photo;
            data.PositionsId = positionsId;
            data.UpdateTime = updateTime;
            return await _dal.UpdateAsync(data);
        }
    }
}
